<?
class ManageImageListForm extends Form
{
    function ManageImageListForm()
    {
        Form::Form('ManageImageListForm');

		$this->link_css('style/ManageImage/manageimage.css');
        $this->link_css("style/paging.css");
    }

    function draw()
    {
        global $display;

		$display->add("WEB_DIR",WEB_DIR);
        $display->add("IMAGE_PATH",IMAGE_PATH);

		if (User::is_login()){
            $display->add("isLogin", true);
            if(!User::have_permit(array("hotel_add", "room_add"))) {
                $display->output('NotPermission');
                return;
            }
            //User::have_permit(array('quyen_check1','quyen_check2','quyen_check3'));
            //User::is_root(); is_admin();
		} else {//if (isset($_COOKIE["GuestID"]) && $_COOKIE["GuestID"]>0){
			//$display->add("isGuest", $_COOKIE["GuestID"]);
            Url::refresh();
            return;
		}

        $this->beginForm(false,'post',false,Url::build_current());
        switch(Url::get('cmd')) {
            case 'delete':
                if($id=Url::getInt('id', 0)){
                    if(!Db_Image::have_permit(array("hotel_add", "room_add"), $id)) {
                        $this->setFormError('Not permission', 'Bạn không có quyền xóa ảnh này.');
                        $display->add("msg", $this->showFormErrorMessages(1));
                    } elseif(!Db_Image::delImageById($id)) {
                        $this->setFormError('Image not found', 'Không xóa được, ảnh không tồn tại hoặc đã bị xóa.');
                        $display->add("msg", $this->showFormErrorMessages(1));
                    }
                } else {
                    $this->setFormError('Image not found', 'Không tìm thấy ảnh cần xóa.');
                    $display->add("msg", $this->showFormErrorMessages(1));
                }
            default:
                $this->showList();
        }

        $this->endForm();
    }
    
    function showList() {
        global $display;
        $isAdmin = User::is_admin();
        $hotels = Db_Hotel::selectHotels('id, name, images', '', $isAdmin?'':'create_id='.User::id());
        if($hotels && count($hotels)) {
            $imgs = array();
            foreach($hotels as &$hotel) {
                $hotel_ids[] = $hotel['id'];
                $image_ids = explode(',', $hotel['images']);
                foreach($image_ids as &$image_id)
                    if(isset($imgs[$image_id]['h'.$hotel['id']]))
                        $imgs[$image_id]['h'.$hotel['id']]['use']++;
                    else
                        $imgs[$image_id]['h'.$hotel['id']] = array('use'=>1, 'prefix'=>'H', 'name'=>$hotel['name'], 'hotel_id'=>$hotel['id']);
            }
            $rooms = Db_Room::getRooms('', 'hotel_id IN (' . implode(',', $hotel_ids) . ')');
            if($rooms && count($rooms)) {
                foreach($rooms as &$room) {
                    $image_ids = explode(',', $room['images']);
                    foreach($image_ids as &$image_id)
                        if(isset($imgs[$image_id]['r'.$room['id']]))
                            $imgs[$image_id]['r'.$room['id']]['use']++;
                        else
                            $imgs[$image_id]['r'.$room['id']] = array('use'=>1, 'prefix'=>'R', 'name'=>$room['name'], 'hotel_id'=>$room['hotel_id']);
                }
            }
        }
        $condition = $isAdmin?'':'user_id='.User::id();
        $total = Db_Image::total($condition);
        $display->add('paging', ECPaging::pagingSE($limit, $total, 20, 10, 'page_no', true, 'ảnh'));

//        var_dump(Url::get('page_no'));die;

        $images = Db_Image::getImages($limit, $condition);
        foreach($images as $key => &$image) {
            if(isset($imgs[$image['id']])) {
                $total_use = 0;
                $img_datas = array();
                foreach($imgs[$image['id']] as &$img) {
                    $total_use += $img['use'];
                    switch($img['prefix']) {
                        case 'H':
                            $img_datas[$img['hotel_id']] = $img;
                            break;
                        case 'R':
                            if(isset($img_datas[$img['hotel_id']])) {
                                $display->add('infoRoom', $img);
                                if(!isset($img_datas[$img['hotel_id']]['rooms']))
                                    $img_datas[$img['hotel_id']]['rooms'] = '';
                                $img_datas[$img['hotel_id']]['rooms'] .= $display->output('InfoRoomImage', true);
                            } else {
                                $img['name'] .= ' ['. $hotels[$img['hotel_id']]['name'] . ']';
                                $img_datas['r'.$img['hotel_id']] = $img;
                            }
                            break;
                    }
                }
                $display->add('total_use', $total_use);
                $display->add('infoHotel', $img_datas);
                $image['info'] = $display->output('InfoHotelsImage', true);
            }
        }
        $display->add('images', $images);
        $display->output('ManageImage');
    }

	function on_submit()
    {
        if(User::is_login() && User::have_permit(array("hotel_add", "room_add"))){
            global $display;
            switch(Url::get('cmd')) {
                case 'save':
                    break;
                default:
                    $ids = Url::get('selected_ids');
                    if(is_array($ids)) {
                        foreach($ids as $k=>$v) {
                            $ids[$k] = (int)$v;
                            if($ids[$k] && !Db_Room::have_permit(array("hotel_add", "room_add"), $ids[$k]))
                                $ids[$k] = 0;
                        }
                        if(!Db_Image::delImages($ids)) {
                            $this->setFormError('Image not found', 'Không tìm thấy ảnh cần xóa hoặc bạn không có quyền xóa.');
                            $display->add("msg", $this->showFormErrorMessages(1));
                        }
                    }
            }
        }
    }
}
?>